//
//  WJADViewController.m
//  BeDeJie
//
//  Created by jerry on 2017/1/26.
//  Copyright © 2017年 jerry. All rights reserved.
//

#import "WJADViewController.h"
#import <AFNetworking/AFNetworking.h>
#import <MJExtension/MJExtension.h>
#import <UIImageView+WebCache.h>
#import "WJADItem.h"
#import "WJTabBarController.h"

#define code2 @"phcqnauGuHYkFMRquANhmgN_IauBThfqmgKsUARhIWdGULPxnz3vndtkQW08nau_I1Y1P1Rhmhwz5Hb8nBuL5HDknWRhTA_qmvqVQhGGUhI_py4MQhF1TvChmgKY5H6hmyPW5RFRHzuET1dGULnhuAN85HchUy7s5HDhIywGujY3P1n3mWb1PvDLnvF-Pyf4mHR4nyRvmWPBmhwBPjcLPyfsPHT3uWm4FMPLpHYkFh7sTA-b5yRzPj6sPvRdFhPdTWYsFMKzuykEmyfqnauGuAu95Rnsnbfknbm1QHnkwW6VPjujnBdKfWD1QHnsnbRsnHwKfYwAwiu9mLfqHbD_H70hTv6qnHn1PauVmynqnjclnj0lnj0lnj0lnj0lnj0hThYqniuVujYkFhkC5HRvnB3dFh7spyfqnW0srj64nBu9TjYsFMub5HDhTZFEujdzTLK_mgPCFMP85Rnsnbfknbm1QHnkwW6VPjujnBdKfWD1QHnsnbRsnHwKfYwAwiuBnHfdnjD4rjnvPWYkFh7sTZu-TWY1QW68nBuWUHYdnHchIAYqPHDzFhqsmyPGIZbqniuYThuYTjd1uAVxnz3vnzu9IjYzFh6qP1RsFMws5y-fpAq8uHT_nBuYmycqnau1IjYkPjRsnHb3n1mvnHDkQWD4niuVmybqniu1uy3qwD-HQDFKHakHHNn_HR7fQ7uDQ7PcHzkHiR3_RYqNQD7jfzkPiRn_wdKHQDP5HikPfRb_fNc_NbwPQDdRHzkDiNchTvwW5HnvPj0zQWndnHRvnBsdPWb4ri3kPW0kPHmhmLnqPH6LP1ndm1-WPyDvnHKBrAw9nju9PHIhmH9WmH6zrjRhTv7_5iu85HDhTvd15HDhTLTqP1RsFh4ETjYYPW0sPzuVuyYqn1mYnjc8nWbvrjTdQjRvrHb4QWDvnjDdPBuk5yRzPj6sPvRdgvPsTBu_my4bTvP9TARqnam"

@interface WJADViewController ()

@property (weak, nonatomic) IBOutlet UIView *adContainView;
@property (weak, nonatomic) IBOutlet UIButton *jumpBtn;

@property (nonatomic, weak) NSTimer *timer;
@property (nonatomic, weak) UIImageView *adImageView;
@property (nonatomic, strong) WJADItem *adItem;

@end

@implementation WJADViewController

//懒加载adImageView
-(UIImageView *) adImageView {
    
    if (_adImageView == nil) {
        UIImageView *imageView = [[UIImageView alloc] init];
        [self.adContainView addSubview:imageView];
        //图片增加点击事件
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapClick)];
        [imageView addGestureRecognizer:tap];
        imageView.userInteractionEnabled = YES;
        
        _adImageView = imageView;
    }
    
    return _adImageView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //加载广告数据
    [self loadAdData];
    
    //开启跳转时的定时器
    self.timer = [NSTimer scheduledTimerWithTimeInterval:1
                                                  target:self
                                                selector:@selector(timeChange)
                                                userInfo:nil
                                                 repeats:YES];
}

//打开网页
-(void) tapClick {
    UIApplication *app = [UIApplication sharedApplication];
    NSURL *jumpUrl = [NSURL URLWithString:self.adItem.jump_url];
    if ([app canOpenURL:jumpUrl]) {
        [app openURL:jumpUrl];
    }
}

-(void) timeChange {
    //倒计时
    static int i=3;
    //倒计时完后，跳转到主界面
    if (i == 0) {
//        [self openMain:nil];
    }
    i--;
    
    //设置跳转，显示的按钮文字
    [self.jumpBtn setTitle:[NSString stringWithFormat:@"跳转(%d)", i] forState:UIControlStateNormal];
}
- (IBAction)openMain:(id)sender {
    //进入主页面，并把主页面设为根控制器。
    WJTabBarController *mainController = [[WJTabBarController alloc] init];
    [UIApplication sharedApplication].keyWindow.rootViewController = mainController;
    
    //销毁定时器
    [self.timer invalidate];
}

//加载广告数据
-(void) loadAdData {
    NSString *url = @"http://mobads.baidu.com/cpro/ui/mads.php";
    
    //1. 创建请求会话管理者
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
    
    //2. 拼接参数
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"code2"] = code2;
    
    //3. 发送请求
    [manager GET:url parameters:params progress:nil
        success:^(NSURLSessionDataTask *task, NSDictionary *responseObject) {
            //模拟数据返回结果
            NSString *filePathPlist=[[NSBundle mainBundle]pathForResource:@"splash_ad" ofType:@"plist"];
            NSDictionary *fileDic = [NSDictionary dictionaryWithContentsOfFile:filePathPlist];
            
            //获取字典
            NSDictionary *adDic = fileDic[@"ad"];
//            NSDictionary *adDic = [responseObject[@"ad"] lastObject];
            
            //字典转模型
            self.adItem = [WJADItem mj_objectWithKeyValues:adDic];
            
            //显示图片
            CGFloat imageHeight = screenWidth /self.adItem.pic_width * self.adItem.pic_height;
            self.adImageView.frame = CGRectMake(0, 0, screenWidth, imageHeight);
            NSURL *picurl = [NSURL URLWithString:self.adItem.pic_url];
            [self.adImageView sd_setImageWithURL:picurl];
            
        } failure:^(NSURLSessionDataTask *task, NSError *error) {
            WJLog(@"%@",error);
    }];
}

@end
